package org.framework;

import org.framework.bean.*;
import org.framework.helper.BeanHelper;
import org.framework.utils.*;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author xuelongjiang
 */
@WebServlet(urlPatterns = "/*",loadOnStartup = 0)
public class DispatcherServlet extends HttpServlet {

    @Override
    public void init(ServletConfig config) throws ServletException {
        //初始化框架
        HelpLoader.init();
        ServletContext servletContext = config.getServletContext();
        ServletRegistration jspServlet = servletContext.getServletRegistration("jsp");
        jspServlet.addMapping(ConfigHelper.getAppJspPath() + "*");

        ServletRegistration defaultServlet = servletContext.getServletRegistration("default");
        defaultServlet.addMapping(ConfigHelper.getAppAssetPath() + "*");
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String requestPath = req.getPathInfo();
        String requestMethod = req.getMethod().toLowerCase();

        //获取处理器
        Hanlder hanlder = ControllerHelp.getHanlder(requestMethod,requestPath);
        if(hanlder != null){
            //获取请求的参数 对应的bean，及方法
            Class<?> controllerClasses = hanlder.getControllerClass();
            Object controllerBean = BeanHelper.getBean(controllerClasses);
            Method method = hanlder.getActionMethod();
            Map<String,Object> paraMap = new HashMap<String,Object>();
            getParaMap(paraMap,req);
            Param param = new Param(paraMap);
            //调用方法
            Object result = ReflectUtil.invokeMethod(controllerBean,method,param);
            if(result instanceof View){

            }else  if(result instanceof Data){

            }

            //返回试图或数据
        }

    }

    private void getParaMap(Map<String,Object> paraMap,HttpServletRequest request) throws IOException{

        Enumeration<String> paramNames = request.getParameterNames();
        while (paramNames.hasMoreElements()){
            String paranName = paramNames.nextElement();
            String paramValue = request.getParameter(paranName);
            paraMap.put(paranName,paramValue);
        }
        String body = CodecUtil.decodeURL(StreamUtil.getString(request.getInputStream()));
        if(StringUtil.isNotEmpty(body)){
            String [] params = StringUtil.splitString(body,"&");
            if(ArrayUtil.isNotEmpty(params)){
                for(String param : params){
                    String [] array = StringUtil.splitString(param,"=");
                    if(ArrayUtil.isNotEmpty(array)){
                        String paramName =array[0];
                        String paramValue = array[1];
                        paraMap.put(paramName,paramValue);
                    }
                }
            }
        }
    }




}
